package process_demo.thread_pool;

/**
 * @author haomin
 * @date 2022/05/03 19:57
 **/
import java.util.Scanner;
import java.util.concurrent.*;

public class Demo {
    public static void main(String[] args) {
        BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1);
        ExecutorService service = new ThreadPoolExecutor(
                3, // 正式员工 10
                9, // 临时员工 20
                10, TimeUnit.SECONDS,
                queue,  // 阻塞队列
                new ThreadFactory() {
                    @Override
                    public Thread newThread(Runnable r) {
                        Thread t = new Thread(r, "饭店厨师");
                        return t;
                    }
                }, // 线程工厂
                new ThreadPoolExecutor.AbortPolicy() // 拒绝策略
        );

        // 定义任务
        Runnable task = new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.DAYS.sleep(365);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        // 把任务提交给线程池对象（公司）
        Scanner s = new Scanner(System.in);
        for (int i = 1; i < 100; i++) {
            s.nextLine();
            service.execute(task);
            System.out.println(i);
        }
    }
}